package com.new2i.util;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 加密处理工具类
 * 
 * @since 2014/09/01
 * @author 2i
 *
 */
public class EncryptionUtil {

	private static int UUIDLENGTH = 32;

	// md5字符串byte数组
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * byte转化string
	 * @since 2014/09/01
	 * @author 2i
	 * @param bByte
	 * @return
	 */
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		// PrintHelper.print("iRet="+iRet);
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}
 

	/**
	 * byte数组转化为string
	 * @since 2014/09/01
	 * @author 2i
	 * @param bByte
	 * @return
	 */
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}

	/**
	 * 将密码md5从65位的字符串分离出来
	 * @param descMD5
	 * @return
	 */
	public static String decodeMD5Desc2Simp(String descMD5) {
		if(descMD5 == null || descMD5.length() < 65){
			return "";
		}
		String sOffset = descMD5.substring(0, 1);
		String sLongMD5 = descMD5.substring(1);
		int offest = Integer.parseInt(sOffset);
		String md5 = sLongMD5.substring(0, offest) + sLongMD5.substring(offest + UUIDLENGTH);
		return md5.toLowerCase();
	}

	/**
	 * 密码加密成65位的字符串
	 * @param pwd
	 * @return
	 */
	public static String encodeMD5Pwd2Desc(String pwd) {
		String pwdMD5 = MD5String(pwd); 
		return encodeMD5Simp2Desc(pwdMD5); 
	}
	
	/**
	 * md5字符串生成65位字符串
	 * @param simpMD5
	 * @return
	 */
	public static String encodeMD5Simp2Desc(String simpMD5) {
		Random random = new Random();
		int index = random.nextInt(9);
		String prefix = simpMD5.substring(0, index);
		String subfix = simpMD5.substring(index); 
		String mixedStr = MD5String(null);
		String descMD5 = index + prefix + mixedStr + subfix;  
		return descMD5.toLowerCase(); 
	}
	
	/**
	 * @Title随机生成16为大小写字母+数字
	 * @return String
	 * */
	public static String getCharAndNumr() {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < 16; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字
			if ("char".equalsIgnoreCase(charOrNum)) // 字符串
			{
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) // 数字
			{
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}
 

	public static boolean isNumber(String str) {
		java.util.regex.Pattern pattern = java.util.regex.Pattern
				.compile("[0-9]*");
		java.util.regex.Matcher match = pattern.matcher(str);
		if (match.matches() == false) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取MD5 hash值
	 * 
	 * @author 2i
	 * @since 2014/09/01
	 * @param strObj
	 * @return
	 */
	public static String MD5String(String str) {
		String uuidStr = UUID.randomUUID().toString().replaceAll("\\-", "");
		if(str == null)	//空对象保证不报错
			str = uuidStr; 
		String resultString = null; 
		try { 
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteToString(md.digest(str.getBytes("utf-8")));
		} catch (Exception ex) {
			ex.printStackTrace(); 
			//出错时也能反返回32位字符串
			while(str.length() < UUIDLENGTH) {
				str += uuidStr; 
			}
			resultString = str.substring(0,UUIDLENGTH);
		}
		return resultString;
	} 
	
	/**
	 * 获取时间戳token
	 * 
	 * @since 2015/09/02
	 * @author 2i
	 * @return
	 */
	public static String getTimestampToken() {
		long current = System.currentTimeMillis();
		String orgTokenStr = UUID.randomUUID().toString() + current; 
		return MD5String(orgTokenStr); 
	}
}
